/*	ResearchStim class definition.
 *
*/

#include "ResearchStim.h"

void ResearchStim::Enter()
{
	DEBUG_PRINT("ResearchStim(%p) %s entering\n", this, GetName().c_str());

}

void ResearchStim::Execute()
{
	DEBUG_PRINT("ResearchStim(%p) %s executing\n", this, GetName().c_str());
	std::list<Base*>* bases = BB->GetBases();
	std::list<BWAPI::Unit*>::iterator upgrader;
	bool found = false;
	for(std::list<Base*>::iterator it = bases->begin(); it != bases->end(); it ++)
	{
		std::list<BWAPI::Unit*>* upgraders = (*it)->GetBuildingsByType(BWAPI::TechTypes::Stim_Packs.whatResearches());
		for(upgrader = upgraders->begin(); upgrader != upgraders->end();  upgrader++)
		{
			if(!(*upgrader)->isResearching())
				break;
		}
		if(upgrader != upgraders->end())
		{
			found = true;
			break;
		}
	}
	if(found)
	{
		BB->SpendGas(BWAPI::TechTypes::Stim_Packs.gasPrice());
		BB->SpendMinerals(BWAPI::TechTypes::Stim_Packs.mineralPrice());
		(*upgrader)->research(BWAPI::TechTypes::Stim_Packs);
	}
	BWAPI::TechTypes::Stim_Packs.whatResearches();
	control->FinishWithSuccess();
}

void ResearchStim::Exit()
{
	DEBUG_PRINT("ResearchStim(%p) %s exiting\n", this, GetName().c_str());
	
}

bool ResearchStim::CheckConditions()
{
	DEBUG_PRINT("ResearchStim(%p) %s checking conditions\n", this, GetName().c_str());
	
	if(BB->isResearch(BWAPI::TechTypes::Stim_Packs))
	{
		DEBUG_PRINT("ResearchStim(%p) %s checked conditions and has %d minerals to spend\n", this, GetName().c_str(), BB->GetMineralsToSpend());
		return true;
	}
	return false;
}